﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Org.BouncyCastle.Asn1.X509;
using PropertyManagementSystemAPI.Common;
using PropertyManagementSystemAPI.Models;

namespace PropertyManagementSystemAPI.Controllers
{
    /// <summary>
    /// 车位费用管理
    /// </summary>
    [Route("api/[controller]")]
    [ApiController]
    [Authorize]
    public class ParkingPriceInfoController : ControllerBase
    {
        private readonly IFreeSql db;

        public ParkingPriceInfoController(IFreeSql _db)
        {
            this.db = _db;
        }

        /// <summary>
        /// 查询车位费用信息
        /// </summary>
        /// <param name="page">页数</param>
        /// <param name="limit">页条数</param>
        /// <param name="state">状态</param>
        /// <param name="strTime">时长</param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult getParkingPrice(int page, int limit, int state, string? strTime)
        {
            bool stateRes = state == 1 ? true : false;
            List<Parkingpriceinfo> list = db.Select<Parkingpriceinfo>()
            .Where(a => ((string.IsNullOrEmpty(strTime)) || (a.Time.Contains(strTime))) && ((state == 2) || (a.State == stateRes)) && a.Isdelete == false)
            .Count(out var total)
            .Page(page, limit)
            .ToList();

            var json = new
            {
                code = 200,
                data = list,
                total = total
            };
            return Ok(json);
        }

        /// <summary>
        /// 添加车位费用信息
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public IActionResult addInfo([FromBody] Parkingpriceinfo info)
        {
            try
            {
                int res = db.Insert(info).ExecuteAffrows();
                return Ok(res);
            }
            catch (Exception)
            {
                return Ok(false);
            }
        }

        /// <summary>
        /// 编辑车位费用信息
        /// </summary>
        /// <returns></returns>
        [HttpPut]
        public IActionResult editInfo([FromBody] Parkingpriceinfo info)
        {
            try
            {
                int res = db.Update<Parkingpriceinfo>().SetSource(info).ExecuteAffrows();
                return Ok(res > 0);
            }
            catch (Exception)
            {
                return Ok(false);
            }
        }

        /// <summary>
        /// 删除车位费用信息
        /// </summary>
        /// <param name="priceid">车位费用id</param>
        /// <returns></returns>
        [HttpDelete]
        public IActionResult deleteInfo(int priceid)
        {
            try
            {
                int res = db.Update<Parkingpriceinfo>(priceid).Set(a => a.Isdelete == true).ExecuteAffrows();
                return Ok(res > 0);
            }
            catch (Exception)
            {
                return Ok(false);
            }
        }

        /// <summary>
        /// 获取车位价格信息绑定下拉框
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [Route("getParkingPriceInfoBindSelect")]
        public IActionResult getParkingPriceInfoBindSelect()
        {
            List<Parkingpriceinfo> list = db.Select<Parkingpriceinfo>().Where(a => a.Isdelete == false).ToList();
            
            List<ReturnParkingPriceInfo> resList = new List<ReturnParkingPriceInfo>();

            for (int i = 0; i < list.Count; i++)
            {
                ReturnParkingPriceInfo info = new ReturnParkingPriceInfo();
                info.Priceid = list[i].Priceid;
                info.PriceInfo = list[i].Time + "：" + list[i].Useprice;
                resList.Add(info);
            }
            return Ok(resList);

        }
    }
}
